package util;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.HashMap;
import java.util.Set;

public class RLUtils {
    private static final String ACCOUNT_SID = "2c94811c8a27cf2d018ac1315eed2af4";
    private static final String AUTH_TOKEN = "f1ed34f8f7b5421b8642afeb3b7bd615";
    private static final String APP_ID = "2c94811c8a27cf2d018acb6b7edb2d36";
    private static final String TEMPLATE_ID = "1";

    public static boolean sendSMS(String phoneNumber, String... datas) {
        boolean resultBool = false;
//        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
//        //请求端口
        String serverPort = "8883";
//        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
//        String accountSId = "2c94811c89c31d000189c39858f80008";
//        String accountToken = "2433abbedf4b4851be6e57d71fc13c2e";
//        //请使用管理控制台中已创建应用的APPID
//        String appId = "2c94811c89c31d000189c3985a9f000f";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(ACCOUNT_SID, AUTH_TOKEN);
        sdk.setAppId(APP_ID);
        sdk.setBodyType(BodyType.Type_JSON);
//        String to = "1352*******";
        String templateId= "1";
//        String[] datas = {"变量1","变量2","变量3"};
//        String subAppend="1234";  //可选 扩展码，四位数字 0~9999
//        String reqId="fadfafas";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        //HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
        HashMap<String, Object> result = sdk.sendTemplateSMS(phoneNumber,templateId,datas);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
            resultBool=true;
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }

        return resultBool;
    }

    public static void main(String[] args) {
//        String phoneNumber = "Recipient Phone Number";
//        String message = "1234";
//        sendSMS(phoneNumber, message);
        String codeStr = String.valueOf((int) ((Math.random()*9+1)*1000));

        sendSMS("13107088626",codeStr,"5");
    }
}
